<!doctype html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
        <style>
         html, body {
             width: 100%;
             height: 100%;
             margin: 0;
             padding: 0;
         }
        </style>
        <script src="../emulators.js"></script>
    </head>
    <body>
        <div>
            <button onClick="javascript:downloadBundleAndRun({worker: true})">Start Worker</button>
            <button onClick="javascript:downloadBundleAndRun({worker: false})">Start Direct</button>
            <pre id="stdout"></pre>
        </div>

        <script>
         emulators.pathPrefix = "../";

         let runId = 0;
         async function runBundle(bundle, options) {
             const id = runId++;
             const stdout = document.getElementById("stdout");

             // promise is resolved when emulator is started
             const ci = await (options.worker ?
                               emulators.dosWorker(bundle) :
                               emulators.dosDirect(bundle));

             ci.events().onStdout((message) => {
                 if (!message.startsWith("dhry2:")) {
                     return;
                 }

                 const [_, runs, delta, vax] = message.split(" ");
                 stdout.innerHTML += (id) + ": " + runs + " runs, browser time " + delta + " ms, " +
                                     "VAX rating " + vax + "\n";
                 if (Number.parseInt(delta, 10) >= 5000) {
                     //properly exit
                     ci.exit();
                 }
             });
         }

         function downloadBundleAndRun(options) {
             const bundleUrl = "dhry2.jsdos";

             // we need to download bundle, emulators accept only Uint8Array
             const xhr = new XMLHttpRequest();
             xhr.open("GET", bundleUrl, true);
             xhr.overrideMimeType("text/plain; charset=x-user-defined");
             xhr.responseType = "arraybuffer";
             xhr.onreadystatechange = () => {
                 if (xhr.readyState === 4 && xhr.status === 200) {
                     // do not forget to create Uint8Array, 
                     // arraybuffer will not work!
                     runBundle(new Uint8Array(xhr.response), options);
                 }
             };
             xhr.send();
         };
        </script>
    </body>
</html>
